<template>
	<view class="">
		<NavBar :title="title" :autoBack="false" id="nav">
			<template #left>
				<view class="navbar-left center" @click="goBack">
					<u-icon name="arrow-left" color="#007aff" size="34"></u-icon>
				</view>
			</template>
		</NavBar>
		<view class="content">
			<view class="block"></view>
			<view class="navbox">
				<u-subsection :list="tabsList" bg-color="#fff" button-color="#fff" v-model="activeIndex"
					@change="changeNav"></u-subsection>
			</view>
			<view class="drop-container">
				<view v-for="(item,index) in showData" :key="index">
					<view class="questions">
						<AnswerEachQuestion :question="item">
						</AnswerEachQuestion>
					</view>
				</view>
				<view class="footer">
					<view class="btn" v-if="+activeIndex!==0" @click.stop="toPre">
						上一步
					</view>
					<view class="btn" v-if="+activeIndex!==2" @click.stop="toNext">
						下一步
					</view>
					<view class="btn" v-if="activeIndex===2" @click.stop="submit">
						提交
					</view>
				</view>
			</view>

		</view>
		<uni-popup ref="message" type="message">
			<uni-popup-message type="warn" :message="messageText" :duration="3000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import AnswerEachQuestion from './components/AnswerEachQuestion.vue'
	import NavBar from "@/component/NavBar"
	// import {
	// 	answerData,
	// } from './data.js'
	import {
		getFormTemplateDetail,
		addResult,
		getTemplateResult,

	} from "@/api/template.js"
	export default {
		name: 'answer',
		components: {
			AnswerEachQuestion,
			NavBar
		},
		computed: {
			showData() {
				return this[this.tabName]
			}
		},
		data() {
			return {
				title: '填写檢查表',
				frontendData: [],
				reviewData: [],
				achievementsData: [],
				tabName: 'frontendData',
				tabsList: [{
						name: '前置工作',
						index: 1,
						tabName: 'frontendData'
					},
					{
						name: '工作檢核',
						index: 2,
						tabName: 'reviewData'
					},
					{
						name: '接績',
						index: 3,
						tabName: "achievementsData"
					},
				],
				activeIndex: 0,
				form_template: '',
				messageText:"",
				casezy_form_template_result_id: '',
				casezy_form_template_id: '',
				casezy_id: '',
			}
		},
		onLoad() {
			if (!uni.getStorageSync("islogin")) {
				this.$Toast("請先登錄")
				setTimeout(() => {
					this.toPage("Home")
				}, 1200)
			} else {
				const {
					casezy_form_template_id,
					casezy_id,
					casezy_form_template_result_id
				} = this.$Route.query
				this.casezy_form_template_id = casezy_form_template_id
				this.casezy_id = casezy_id
				this.casezy_form_template_result_id = casezy_form_template_result_id
				if(this.casezy_form_template_result_id){ // 有这个id，代表是继续答题，之前答过了，需要获取之前的答题数据
					this.getHistoryDetail()
				}else if(this.casezy_form_template_id) {
					this.getDetail()
				}
			}
		},
		// created() {
		// 	this.frontendData = answerData.frontendData
		// 	this.reviewData = answerData.reviewData
		// 	this.achievementsData = answerData.achievementsData
		// },
		methods: {
			goBack() {
				uni.navigateBack(-1)
			},
			changeNav(index) {
				this.tabName = this.tabsList[index].tabName
				console.log(index, this.tabName)
			},
			toNext() {
				switch (this.tabName) {
					
					case 'frontendData':
						// let res = this.validateInput(this.frontendData, '前置工作')
						// if (!res) {
						// 	return
						// }
						this.tabName = 'reviewData'
						break;
					case 'reviewData':
						// let res1 = this.validateInput(this.reviewData, '工作檢核')
						// if (!res1) {
						// 	return
						// }
						// this.$goTop()
						this.tabName = 'achievementsData'
						// this.activeIndex++
						break;
				}
				this.$goTop()
				this.activeIndex++
			},
			toPre() {
				switch (this.tabName) {
					case 'reviewData':
						this.tabName = 'frontendData'
						break;
					case 'achievementsData':
						this.tabName = 'reviewData'
						break;
				}
				this.$goTop()
				this.activeIndex--
			},
			getDetail() {
				let data = {
					casezy_form_template_id: this.casezy_form_template_id,
					uid: uni.getStorageSync("uid"),
				}
				getFormTemplateDetail(data).then((res) => {
					try {
						let resData = res.form_template
						let jsonStr = resData.form_template_content.replace(/&quot;/g, '"')
						let obj = JSON.parse(jsonStr)
						// let obj = JSON.parse(resData.form_template_content)
						this.form_template = res.form_template
						this.frontendData = obj.frontendData
						this.achievementsData = obj.achievementsData
						this.reviewData = obj.reviewData
					} catch (err) {
						console.log(err)
					}
				}).catch((err) => {
					if (err.code === 102) {
						this.$ClearLogin(err.msg)
					} else {
						this.$Toast(err.msg)
					}
				})
			},
			getHistoryDetail() {
				let data = {
					casezy_form_template_result_id: this.casezy_form_template_result_id,
					uid: uni.getStorageSync("uid"),
				}
				getTemplateResult(data).then((res) => {
					try {
						let resData = res.form_template_result
						let obj = JSON.parse(resData)
						this.form_template = JSON.parse(res.form_template_content)
						this.frontendData = obj.frontendData
						this.achievementsData = obj.achievementsData
						this.reviewData = obj.reviewData
					} catch (err) {
						console.log(err)
					}
				}).catch((err) => {
					if (err.code === 102) {
						this.$ClearLogin(err.msg)
					} else {
						this.$Toast(err.msg)
					}
				})
			},
			validateInput(data, tableName) {
				let flag = true
				for (let i = 0; i < data.length; i++) {
					for (let j = 0; j < data[i].dataList.length; j++) {
						let curJ = data[i].dataList[j]
						console.log(data[i],data[i].moduleName)
						if(curJ.type==='radio'&&!curJ.value){
							this.messageText = `${tableName}中${data[i].moduleName}的第${j+1}題未作答`
							this.$refs.message.open()
							flag = false
							break;
						}
						if(curJ.type==='select'){
							let find = curJ.options.find((v)=> v.checked)
							if(!find){
								this.messageText = `${tableName}中${data[i].moduleName}的第${j+1}題未作答`
								this.$refs.message.open()
								flag = false
								break;
							}
						}
						if(curJ.type==='upload'&&!curJ.fileList.length){
							this.messageText = `${tableName}中${data[i].moduleName}的第${j+1}題未作答`
							this.$refs.message.open()
							flag = false
							break;
						}
						if ((
							curJ.type === 'date' ||
							curJ.type === 'time' ||
							curJ.type === 'input'
							)&&!curJ.value
						) {
							this.messageText = `${tableName}中${data[i].moduleName}的第${j+1}題未作答`
							this.$refs.message.open()
							flag = false
							break;
						}
					}
					if (!flag) {
						break
					}
				}
				return flag

			},
			submit() {
				// let res = this.validateInput(this.frontendData, '前置工作')
				// if (!res) {
				// 	return
				// }
				// let res1 = this.validateInput(this.reviewData, '工作檢核')
				// if (!res1) {
				// 	return
				// }
				// let res2 = this.validateInput(this.achievementsData,'接績')
				// if(!res2){
				// 	return
				// }
				let obj = {
					frontendData: this.frontendData,
					reviewData: this.reviewData,
					achievementsData: this.achievementsData
				}
				let pramas = {
					uid: uni.getStorageSync("uid"),
					casezy_form_template_id: this.casezy_form_template_id,
					form_template_result: JSON.stringify(obj),
					form_template_content: JSON.stringify(this.form_template),
					casezy_form_template_result_id: this.casezy_form_template_result_id,
				}
				addResult(pramas).then((res) => {
					this.$Toast('操作成功')
					setTimeout(() => {
						this.goBack()
					}, 300)

				}).catch((err) => {
					if (err.code === 102) {
						this.$ClearLogin(err.msg)
					} else {
						this.$Toast(err.msg)
					}
				})
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.block {
		height: 100rpx;
	}

	.tabs {
		display: flex;
		justify-content: space-around;

		.tab {
			width: 360rpx;
			height: 64rpx;
			line-height: 64rpx;
			font-weight: 400;
			color: #333333;
			margin-bottom: 16rpx;
			text-align: center;
			cursor: pointer;
		}

		.active {
			background: #FFFFFF;
			box-shadow: 0rpx 2rpx 2rpx 1rpx rgba(197, 197, 197, 0.25);
			border-radius: 0rpx 0rpx 0rpx 0rpx;
		}
	}

	.content {
		margin: 0 auto;
		border: 1px solid #F6F6F6;
		border-radius: 10rpx;
		max-width: 1800rpx;
	}

	.navbox {
		margin: 0 auto;

		/deep/ .u-item {
			position: relative;

			&::after {
				content: "";
				width: 2rpx;
				height: 32rpx;
				background-color: #f1f1f1;
				position: absolute;
				right: 0rpx;
			}

			&.u-item-3::after {
				display: none;
			}
		}

		/deep/ .u-item-bg {
			box-shadow: 0rpx 4rpx 4rpx rgba(0, 0, 0, .1);
		}
	}

	.footer {
		display: flex;
		justify-content: flex-end;
		padding: 0 24rpx;
		gap: 24rpx;
		margin-top: 24rpx;

		.btn {
			color: #fff;
			cursor: pointer;
			background: #3A78FC;
			border-radius: 6px;
			padding: 12rpx 24rpx;
		}
	}
	::v-deep .fixforpc-z-index {
		z-index: 99999;
	}
</style>